//
//  WDDictToModel.m
//  ArtPage项目实战
//
//  Created by 王笛 on 16/9/6.
//  Copyright © 2016年 diwang. All rights reserved.
//

#import "WDDictToModel.h"
#import "FMDatabase.h"

@implementation WDDictToModel

//字典转模型
+ (NSMutableArray *)dictToModelWithModel:(NSString *)className andDict:(id)array
{
    NSMutableArray *models = [[NSMutableArray alloc] initWithCapacity:1];
    
    if ([array isKindOfClass:[NSArray class]]) {
        
        for(int i = 0; i < [array count]; ++i)
        {
            id dict = array[i];
            if(![dict isKindOfClass:[NSDictionary class]])
            {
                continue;
            }
            
            id model = [[NSClassFromString(className) alloc] init];
            
            for(NSString *propertyName in [self propertysToArray:className])
            {
                id value = [dict objectForKey:propertyName];
                
                if(!value || [value isKindOfClass:[NSNull class]])
                {
                    continue;
                }
                
                [model setValue:value forKey:propertyName];
            }
            
            [models addObject:model];
            
        }
    } else {
        id dict = array;
        
        if(![dict isKindOfClass:[NSDictionary class]])
        {
            return 0;
        }
        
        id model = [[NSClassFromString(className) alloc] init];
        
        for(NSString *propertyName in [self propertysToArray:className])
        {
            id value = [dict objectForKey:propertyName];
            
            if(!value || [value isKindOfClass:[NSNull class]])
            {
                continue;
            }
            
            [model setValue:value forKey:propertyName];
        }
        
        [models addObject:model];
    }
    
    
    return models;
}

//把模型转数据库类型的字典
+ (NSDictionary *)transformToDictionaryWithModle:(NSString *)className
{
    
    NSMutableDictionary *objects = [NSMutableDictionary dictionary];
    
    for(NSString *propertyName in [self propertysToArray:className])
    {
        [objects setObject:@"text" forKey:propertyName];
    }
    
    return objects;
}

//数据库查询集合转化为模型对象
+ (id)dataSetToModelWithResultSet:(id)resultSet andModel:(NSString *)className
{
    id model = [[NSClassFromString(className) alloc] init];
    
    for(NSString *key in [self propertysToArray:className])
    {
        NSString *value = [resultSet stringForColumn:key];
        
        if(value && ![value isKindOfClass:[NSNull class]])
        {
            [model setValue:value forKey:key];
        }
        
    }
    
    return model;
}

//模型属性存到数组
+ (NSArray *)propertysToArray:(NSString *)className
{
    //1.取出类中所有实例变量
    unsigned int outCount = 0;
    objc_property_t *propertys = class_copyPropertyList([NSClassFromString(className) class], &outCount);
    NSMutableArray *propertysArr = [NSMutableArray array];
    
    //2.遍历所有实例变量
    for(int i = 0; i < outCount; i++)
    {
        objc_property_t property = propertys[i];
        NSString *propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
        [propertysArr addObject:propertyName];
    }
    
    return propertysArr;
}

@end
